/*
 * Copyright (c) 2025 zhangxiang (fishlikewater@126.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.github.fishlikewater.raiden.http.core.factory;

import io.github.fishlikewater.raiden.http.core.client.DefaultTrustManager;
import io.github.fishlikewater.raiden.http.core.exception.HttpExceptionCheck;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.net.http.HttpClient;
import java.time.Duration;

/**
 * {@code HttpClientFactory}
 *
 * @author zhangxiang
 * @version 1.1.3
 * @since 2025/01/21
 */
public class HttpClientFactory {

    public static HttpClient httpsClient() {
        SSLContext ctx;
        try {
            ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new DefaultTrustManager();
            ctx.init(null, new TrustManager[]{tm}, null);
        } catch (Exception e) {
            return HttpExceptionCheck.INSTANCE.throwUnchecked(e);
        }
        return HttpClient.newBuilder()
                .sslContext(ctx)
                .connectTimeout(Duration.ofSeconds(60))
                .version(HttpClient.Version.HTTP_1_1)
                .build();
    }
}
